package com.itchen.config;

import com.itchen.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC 配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final JwtInterceptor jwtInterceptor;

    public WebMvcConfig(JwtInterceptor jwtInterceptor) {
        this.jwtInterceptor = jwtInterceptor;
    }

//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        // 指定controller统一的接口前缀
//        configurer.addPathPrefix("/api", clazz -> clazz.isAnnotationPresent(RestController.class));
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册JWT拦截器
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**") // 拦截所有请求
                .excludePathPatterns("/api/auth/login") // 排除登录接口
                .excludePathPatterns("/api/auth/register") // 排除注册接口
                .excludePathPatterns("/api/file/**") // 排除文件上传接口
                .excludePathPatterns("/doc.html", "/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui/**") // 排除Swagger相关接口
                .excludePathPatterns("/error"); // 排除错误页面
    }
} 